package view;
/**
 *
 * @author zhaoyingxi
 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumnModel;
import java.util.List;
import model.AirLine;
import Control.AirLineControl;
import javax.swing.JComponent;




public class AirLineBookViewT {
    private JFrame frame = null;

       private JTable table = null;

       private Book_Model model = null;

       private JScrollPane s_pan = null;

       private JButton button_1 = null, button_2 = null, button_3 = null;

       private JPanel pane = null;
       public AirLineBookViewT() {

           AirLineControl airLineControl = new AirLineControl();
           List<AirLine> list = airLineControl.find();
           frame = new JFrame("AirLineManagerView");

           pane = new JPanel();

           button_1 = new JButton("Booking");

           button_1.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
//                  model.content
                   BookView bookView =new BookView(model.content,table.getSelectedRow(),model);
               }
           });
           pane.add(button_1);
           model = new Book_Model(20);
           table = new JTable(model);
           //
           table.setBackground(Color.white);
           TableColumnModel tcm = table.getColumnModel();
           tcm.getColumn(0).setPreferredWidth(50);
           tcm.getColumn(1).setPreferredWidth(100);
           tcm.getColumn(2).setPreferredWidth(50);
           for(int i=0;i<list.size();i++)
           {
               this.addData(list.get(i));
           }
           s_pan = new JScrollPane(table);
           frame.add(s_pan, BorderLayout.CENTER);
           frame.add(s_pan, BorderLayout.CENTER);
           frame.add(pane, BorderLayout.NORTH);
           frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
           frame.setSize(frame.getMaximumSize());
           frame.setResizable(false);
           frame.setVisible(true);
       }
       private void addData(AirLine airline) {
       Integer levelNum = airline.getMax() - airline.getSoldNum();
       model.addRow(airline.getAirLineNum()+"", airline.getFrom()+"",  airline.getTo()+"",airline.getPrice()+""
                    , airline.getDiscountPrice()+"",  airline.getDepartureTime()+"", airline.getDiscountNum()+"",levelNum+"");
       table.updateUI();
       }
       private void removeData() {
                int a =  table.getSelectedRow();
                String num = (String) model.getValueAt(a,0);
                System.out.print(num);
                AirLineControl airLineControl = new AirLineControl();
                airLineControl.deleteAirLine(num);
                model.removeRow(a);
                table.updateUI();
//        model.removeRows(0, model.getRowCount());
//        table.updateUI();
       }
       //
       private void saveData() {
           int col = model.getColumnCount();
           int row = model.getRowCount();
           for (int i = 0; i < col; i++) {
               System.out.print(model.getColumnName(i) + "\t");
           }
           System.out.print("\r\n");
           for (int i = 0; i < row; i++) {
               for (int j = 0; j < col; j++) {
                   System.out.print(model.getValueAt(i, j) + "\t");
               }
               System.out.print("\r\n");
           }
           System.out.println("------------------------------------");
       }

       public static void main(String args[]) {
           new AirLineManagerViewT();
       }

   }

/**
 *
 * @author zhaoyingxi
 */
   class Book_Model extends AbstractTableModel {

       private static final long serialVersionUID = -7495940408592595397L;

       public Vector content = null;

       private String[] title_name = { "AIRLINE NUM", "FROM", "TO", "PRICE", "DISCOUNT PRICE","DEPARTURE TIME","DISCOUNT NUM","LEVEL TICKETS",};

       public Book_Model() {
           content = new Vector();
       }
       public Book_Model(int count) {
           content = new Vector(count);
       }

       public void addRow(String airlineNum,String from,String to,String price,String discountPrice,String depatureTime,String discountNum,String levelNum) {
           Vector v = new Vector(4);
           v.add(0, airlineNum);
           v.add(1, from);
           v.add(2, to);
           v.add(3, price);
           v.add(4, discountPrice);
           v.add(5, depatureTime);
           v.add(6, discountNum);
           v.add(7, levelNum);

           content.add(v);
       }

       public void removeRow(int row) {
           content.remove(row);
       }

       public void removeRows(int row, int count) {
           for (int i = 0; i < count; i++) {
               if (content.size() > row) {
                   content.remove(row);
               }
           }
       }
/**
 *
 * @author zhaoyingxi
 */
       public boolean isCellEditable(int rowIndex, int columnIndex) {

           return false;
       }


       public void setValueAt(Object value, int row, int col) {
           ((Vector) content.get(row)).remove(col);
           ((Vector) content.get(row)).add(col, value);
           this.fireTableCellUpdated(row, col);
       }

       public String getColumnName(int col) {
           return title_name[col];
       }

       public int getColumnCount() {
           return title_name.length;
       }

       public int getRowCount() {
           return content.size();
       }

       public Object getValueAt(int row, int col) {
           return ((Vector) content.get(row)).get(col);
       }

       public Class getColumnClass(int col) {
           return getValueAt(0, col).getClass();
       }
}
